#pragma once
#include "player.h"
#include "sun_bullet.h"
#include "sun_bullet_ex.h"

extern Atlas atlas_sunflower_idle_left;
extern Atlas atlas_sunflower_idle_right;
extern Atlas atlas_sunflower_run_left;
extern Atlas atlas_sunflower_run_right;
extern Atlas atlas_sunflower_attack_ex_left;
extern Atlas atlas_sunflower_attack_ex_right;
extern Atlas atlas_sun_text;
extern Atlas atlas_sun_text_reverse;
extern Atlas atlas_sunflower_die_left;
extern Atlas atlas_sunflower_die_right;

extern player* player_1;
extern player* player_2;

extern std::vector<Bullet*> bullet_list;

class SunflowerPlayer : public player
{
public:
	SunflowerPlayer()
	{
		animation_idle_left.set_atlas(&atlas_sunflower_idle_left);
		animation_idle_right.set_atlas(&atlas_sunflower_idle_right);
		animation_run_left.set_atlas(&atlas_sunflower_run_left);
		animation_run_right.set_atlas(&atlas_sunflower_run_right);
		animation_attack_ex_left.set_atlas(&atlas_sunflower_attack_ex_left);
		animation_attack_ex_right.set_atlas(&atlas_sunflower_attack_ex_right);
		animation_sun_text.set_atlas(&atlas_sun_text);
		animation_sun_text_reverse.set_atlas(&atlas_sun_text_reverse);
		animation_die_left.set_atlas(&atlas_sunflower_die_left);
		animation_die_right.set_atlas(&atlas_sunflower_die_right);

		animation_attack_ex_left.set_interval(100);
		animation_attack_ex_right.set_interval(100);
		animation_sun_text.set_interval(100);
		animation_sun_text_reverse.set_interval(100);
		animation_idle_left.set_interval(100);
		animation_idle_right.set_interval(100);
		animation_run_left.set_interval(100);
		animation_run_right.set_interval(100);
		animation_die_left.set_interval(150);
		animation_die_right.set_interval(150);

		animation_attack_ex_left.set_loop(false);
		animation_attack_ex_right.set_loop(false);
		animation_sun_text.set_loop(false);
		animation_sun_text_reverse.set_loop(false);
		animation_die_left.set_loop(false);
		animation_die_right.set_loop(false);

		animation_attack_ex_left.set_callback([&]()
			{
				is_attack_ex = false;
				is_sun_text_visible = false;
			});
		animation_attack_ex_right.set_callback([&]()
			{
				is_attack_ex = false;
				is_sun_text_visible = false;
			});

		animation_sun_text.set_callback([&]() 
			{
				is_attack_ex = false; 
				is_sun_text_visible = false; 
			});
		animation_sun_text_reverse.set_callback([&]() 
			{
				is_attack_ex = false;
				is_sun_text_visible = false;
			});

		size = { 96,96 };

		attack_cd = 250;
	}
	~SunflowerPlayer() = default;

	void on_update(int delta)
	{
		player::on_update(delta);
		if (is_sun_text_visible)
		{
			is_facing_right ? animation_attack_ex_right.on_update(delta) :
				animation_attack_ex_left.on_update(delta);
			!is_facing_right ? animation_sun_text.on_update(delta) : animation_sun_text_reverse.on_update(delta);
		}
	}
	void on_draw(const Camera& camera)
	{
		if (is_sun_text_visible)
		{
			is_facing_right ? animation_attack_ex_right.on_draw(camera, (int)get_position().x, (int)get_position().y) :
				animation_attack_ex_left.on_draw(camera, (int)get_position().x, (int)get_position().y);
			Vector2 text_position;
			IMAGE* frame = animation_sun_text.get_frame();
			text_position.x = position.x - (size.x - frame->getwidth()) / 2;
			text_position.y = position.y - frame->getheight();
			is_facing_right ? animation_sun_text_reverse.on_draw(camera, (int)text_position.x, (int)text_position.y) :
			animation_sun_text.on_draw(camera, (int)text_position.x, (int)text_position.y);
		}
		else
			player::on_draw(camera);
	}
	void on_attack()
	{
		Bullet* bullet = new SunBullet();

		Vector2 bullet_position;
		const Vector2& bullet_size = bullet->get_size();
		bullet_position.x = position.x + (size.x - bullet_size.x) / 2;
		bullet_position.y = position.y;

		bullet->set_position(bullet_position.x, bullet_position.y);
		bullet->set_speed(is_facing_right ? speed_sun.x : -speed_sun.x, speed_sun.y);

		bullet->set_collide_target(id == PlayerID::P1 ? PlayerID::P2 : PlayerID::P1);

		bullet->set_callback([&]() {mp += 35; });

		bullet_list.push_back(bullet);
	}
	void on_attack_ex()
	{
		is_attack_ex = true;
		is_sun_text_visible = true;

		animation_sun_text.reset();
		is_facing_right ? animation_attack_ex_right.reset() : animation_attack_ex_left.reset();

		Bullet* bullet = new SunbulletEx();
		player* target_player = (id == PlayerID::P1 ? player_2 : player_1);

		Vector2 bullet_position, bullet_speed;
		const Vector2& bullet_size = bullet->get_size();
		const Vector2& target_size = target_player->get_size();
		const Vector2& target_position = target_player->get_position();
		bullet_position.x = target_position.x + (target_size.x - bullet_size.x) / 2;
		bullet_position.y = -size.y;
		bullet_speed.x = 0;
		bullet_speed.y = speed_sun_ex;

		bullet->set_position(bullet_position.x, bullet_position.y);
		bullet->set_speed(bullet_speed.x, bullet_speed.y);

		bullet->set_collide_target(id == PlayerID::P1 ? PlayerID::P2 : PlayerID::P1);

		bullet->set_callback([&]() {mp += 50; });
		bullet_list.push_back(bullet);

		mciSendString(L"play sun_text from 0", NULL, 0, NULL);
	}
private:
	const float speed_sun_ex = 0.15f;
	const Vector2 speed_sun = { 0.25f,-0.5f };

	Animation animation_sun_text;
	Animation animation_sun_text_reverse;
	bool is_sun_text_visible = false;
};